import copy
from concurrent.futures import ThreadPoolExecutor
from time import time
from multiprocessing import Process, Queue

from utils.logging_tool.log_control import Listing_Error
from utils.mysqlUtils.db_utils import DB
from utils.noticeUtils.dingtalkControl import DingTalkSendMsg
from public_way import public_invoking
from utils.times_tool.time_control import now_time


class TestIsHotDataServer:
    """
    自动刊登同步服务
    - 自动刊登Listing数据同步至冷数据库
    """
    public_program = public_invoking()

    def autopublish_verify(self, id, time):

        # star_time = time()

        id_list = id.split(',')
        for e in id_list:
            # 获取大宽表增量自动刊登的Listing数据
            erp_sql = f"SELECT OrdersourceID, OrderSourceSKUID FROM RB_Amazon_Product_Base " \
                      f"WHERE CustomerID = 1 AND IsDelete = 0 AND OrderSourceID IN ({e}) AND OnShelfMode IN ( 9 ) " \
                      f"AND IsOffShelf <= 2 AND AddTime {time};"
            new_publish_listing_erp = self.public_program.erp_db(erp_sql)

            # 根据渠道ID获取来源渠道对应的部门表ID
            ordersource_sql = f"select DepartmentId from aml_department_ordersource where OrderSourceId in ({e})"
            baseid = self.public_program.cold_db(ordersource_sql)[0]["DepartmentId"]

            # 创建线程池
            pool = ThreadPoolExecutor()
            for data in new_publish_listing_erp:
                pool.submit(self.data_comparison, data, baseid)
            pool.shutdown()

        # end_time = time()
        # print(f"检测完成,耗时{round(end_time - star_time, 2)}")

    def data_comparison(self, data, baseid):

        sql = f"SELECT COUNT(*) FROM aml_amazon_product_base_dep{baseid} " \
              f"WHERE OrderSourceSKUID = {data['OrderSourceSKUID']} AND IsHotData = 0 AND OrderSourceID IN ({data['OrdersourceID']});"
        cold_base = DB("mysql_db_code_base")
        cold_res = cold_base.execute_sql_many(sql)
        if cold_res[0]["COUNT(*)"] == 0:
            Listing_Error.logger.error(f"{data}>>>>>>>>>存在异常")
        else:
            print(f"{data}>>>>>>>>>正常")


if __name__ == '__main__':

    # 自动刊登同步服务
    id = '12792,5005,14843'
    time = "BETWEEN '2023-3-2 00:00:00' AND '2023-3-8 23:59:59'"
    TestIsHotDataServer().autopublish_verify(id, time)
